//
//  NMeTableViewController.m
//  News
//
//  Created by zhangtao on 2018/10/7.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "NMeTableViewController.h"
#import "NMeHeadCell.h"
#import "NMeTableViewCell.h"
#import "NUserInfoViewController.h"
#import "NMyFocusViewController.h"
#import "NMyCollectViewController.h"
#import "NMyMsgViewController.h"
#import "NMyArticleViewController.h"
#import "NMySetTableViewController.h"
@interface NMeTableViewController ()

@end

@implementation NMeTableViewController
{
    NSArray *titleArray;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self requestSummary];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self setHidesBottomBarWhenPushed:NO];
}

-(instancetype)initWithStyle:(UITableViewStyle)style{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.bounces = NO;
    self.tableView.backgroundColor = RGB(249, 249, 249, 1);
    [self.tableView registerNib:[UINib nibWithNibName:@"NMeHeadCell" bundle:nil] forCellReuseIdentifier:@"headCell"];
    self.tableView.separatorColor = RGB(247, 247, 247, 1);
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
 }

-(void)requestSummary
{
    NSDictionary *param = @{@"token":TOKEN,@"cuid":CUID};
    [[NHTTPManage shareInstance]afGet:HTTP_GET_SUMMARY andParam:param success:^(id  _Nonnull responseObject) {
        NSLog(@"..");
        NSummaryModel *model = [NSummaryModel shareInstance];
        [model initWithDic:responseObject[@"Data"]];
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    } failture:^(id  _Nonnull error) {
        NSLog(@".");
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ((NSArray *)self.titleArray[section]).count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 200;
    }
    else{
        return 40;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return .1;
    }else{
        return 10;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [UIView new];
    }
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, 10)];
    view.backgroundColor = RGB(245, 245,245, 1);
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        NMeHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headCell"];
        [cell bindData];
        cell.meHeadCellCallBack = ^(NSInteger tag) {
            [self jumpToControllerWithTag:tag];
        };
        return cell;
    }else{
        NMeTableViewCell *cell = [[NMeTableViewCell alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, 45)];
        cell.textLabel.font = BASE_FONT;
        cell.textLabel.text = self.titleArray[indexPath.section][indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.markInfo = @"123条新评论";
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([cell.textLabel.text isEqualToString:@"设置"]) {
        NMySetTableViewController *set = [[NMySetTableViewController alloc]init];
        [self.navigationController pushViewController: set animated:YES];
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            NMyMsgViewController *msg = [[NMyMsgViewController alloc]init];
            msg.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:msg animated:YES];
        }else if (indexPath.row == 1){
            [self jumpToControllerWithTag:0];
        }else if (indexPath.row == 2){
            [self jumpToControllerWithTag:1];
        }else if (indexPath.row == 3){
            NWebViewController *vc = [[NWebViewController alloc] init];
            vc.netHtml = [NSString stringWithFormat:@"%@%@?token=%@&cuid=%@",baseH5Url,H5_MyPost,TOKEN,CUID];
            vc.title = @"我的文章";
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
//            [self jumpWithControllerName:@"NMyArticleViewController"];
        }else if(indexPath.row == 4){
            NWebViewController *vc = [[NWebViewController alloc] init];
            vc.netHtml = [NSString stringWithFormat:@"%@%@?token=%@&cuid=%@",baseH5Url,H5_MyTopic,TOKEN,CUID];
            vc.title = @"我的主题";
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
//            [self jumpWithControllerName:@"NMyThemeMainController"];

        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 1) {
            
        } else if (indexPath.row == 0) {
            NWebViewController *vc = [[NWebViewController alloc] init];
            vc.netHtml = [NSString stringWithFormat:@"%@%@",baseH5Url,H5_help];
            vc.title = @"帮助中心";
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

-(void)jumpToControllerWithTag:(NSInteger)tag{
    if (tag == 0) {
        [self jumpWithControllerName:@"NMyFocusViewController"];
    }else if (tag == 1){
        [self jumpWithControllerName:@"NMyCollectViewController"];
    }else if (tag == 2){
        NSLog(@"我的收藏");
    }
    else if (tag == 3){
        [self jumpWithControllerName:@"NUserInfoViewController"];
        
    }
}

-(void)jumpWithControllerName:(NSString *)name{
  
    UIViewController *controller = [[NSClassFromString(name) alloc]init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:nil];
}

-(NSArray *)titleArray{
    if (!titleArray) {
        titleArray = @[@[@""],@[@"我的消息",@"我的关注",@"我的收藏",@"我的文章",@"我的主题"],@[@"帮助中心",@"设置"]];
    }
    return titleArray;
}

@end
